package com.storydo.file.share.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Data;

/**   
* @Title: Table.java 
* @Description: TODO
* @author : Storydo
* @date: 2021年3月12日 上午11:06:05 
* @version:V1.0 
* Copyright 悦享互联 2016 All right reserved.
* Modification  History:
* Version       Date          Author          Description
* ----------------------------------------------------------------------------
*  1.0         2021年3月12日        Storydo              TODO
*/

public class TableMap {

	private Map<String, Table> nameMap;
	
	private Map<String, Table> propMap;
	
	public TableMap(List<String> names, List<String> props) {
		super();
		nameMap = new HashMap<>();
		for (int i = 0; i < names.size(); i++) {
			Table t = new Table();
			t.setName(names.get(i));
			t.setProperty(props.get(i));
			nameMap.put(t.getName(), t);
		}
	}
	
	public void setHeader(String firstLine) {
		setHeader(firstLine.split(";"));
	}
	
	public void setHeader(String[] header) {
		for (int i = 0; i < header.length; i++) {
			String head = header[i];
			Table t = nameMap.get(head);
			if (t == null) {
				continue;
			}
			t.setIndex(i);
		}
	}
	
	public int getPropIndex(String prop) {
		return getPropMap().get(prop).getIndex();
	}
	
	public Map<String, Table> getNameMap() {
		return new HashMap<>(nameMap);
	}
	
	public Map<String, Table> getPropMap() {
		if(propMap != null) {
			return propMap;
		}
		propMap = new HashMap<>();
		for (Table t : nameMap.values()) {
			propMap.put(t.getProperty(), t);
		}
		
		return propMap;
	}
	
	@Data
	public class Table {
		private String name;
		private String property;
		private Integer index;
	}
	
	
}
